package com.yiyi.credit.manage.abutment.service;

import com.yiyi.credit.manage.abutment.common.enums.ResponseCode;
import com.yiyi.credit.manage.abutment.common.exceptions.ServiceException;
import com.yiyi.credit.manage.abutment.mapper.DictInfoMapper;
import com.yiyi.credit.manage.model.entity.DictInfo;
import com.yiyi.credit.manage.model.vo.DictInfoVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;


@Service
public class DictInfoService {

    @Autowired
    private DictInfoMapper dictInfoMapper;


    public List<DictInfo> queryDictInfoByCode(Integer code) throws ServiceException {

        return dictInfoMapper.queryDictInfoByCode(code);
    }


    public List<DictInfoVo> queryDictInfoByCodes(String codes) throws ServiceException {
        if (StringUtils.isEmpty(codes)) {
            throw new ServiceException(ResponseCode.PARAM_ERR);
        }
        List<DictInfoVo> dictInfoVoList = new ArrayList<>();
        String[] codeList = codes.split(",");
        if (codeList.length > 0) {
            for (String code : codeList) {
                DictInfoVo dictInfoVo = new DictInfoVo();
                dictInfoVo.setCode(Integer.parseInt(code));
                List<DictInfo> dictInfoList = dictInfoMapper.queryDictInfoByCode(Integer.parseInt(code));
                dictInfoVo.setDictInfoList(dictInfoList);
                dictInfoVoList.add(dictInfoVo);
            }
        }
        return dictInfoVoList;
    }


    public List<DictInfo> queryAllDictInfo() throws ServiceException {

        return dictInfoMapper.selectAll();
    }
}
